package cn.wolf.notify;

import java.util.Random;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import cn.wolf.notify.NotificationEntity.Message;
import cn.wolf.tools.Log;

/**
 * <h3>底层消息接收器</h3>
 * <p>接收到消息，并分发给状态栏进行展示</p>
 */
public final class NotificationReceiver extends BroadcastReceiver {
	public static final String NOTIFICATION_SHOW_ACTION = "cn.wolf.notify.NotificationReceiver";

	@Override
	public void onReceive(Context context, Intent intent) {
		Log.d("NotificationReceiver.onReceive()...");
		String action = intent.getAction();
		Log.d("action=" + action);

		NotificationIQ iq = new NotificationIQ();
		int id = intent.getIntExtra(NotificationConstants.NOTIFICATION_ID,
				new Random(System.currentTimeMillis()).nextInt());
		String title = intent.getStringExtra(NotificationConstants.NOTIFICATION_TITLE);
		String content = intent.getStringExtra(NotificationConstants.NOTIFICATION_CONTENT);
		Message message = (Message) intent
				.getSerializableExtra(NotificationConstants.NOTIFICATION_MESSAGE);
		String className = intent.getStringExtra(NotificationConstants.NOTIFICATION_CLASS);
		int iconId = intent.getIntExtra(NotificationConstants.NOTIFICATION_ICON, -1);
		boolean isSound = intent
				.getBooleanExtra(NotificationConstants.SETTINGS_SOUND_ENABLED, true);
		boolean isVibrate = intent.getBooleanExtra(NotificationConstants.SETTINGS_VIBRATE_ENABLED,
				true);
		boolean notificationEnable = intent.getBooleanExtra(
				NotificationConstants.SETTINGS_NOTIFICATION_ENABLED, true);

		iq.setId(id);
		iq.setTitle(title);
		iq.setContent(content);
		iq.setMessage(message);
		iq.setIcon(iconId);
		iq.setSound(isSound);
		iq.setVibrate(isVibrate);
		iq.setEnable(notificationEnable);

		Notifier notifier = new Notifier(context);
		notifier.notify(iq, className);
	}

}
